<?php
/*
 * 登录model
*/
namespace app\admin\model;
use think\Cache;
use \think\Model;
use \think\db;


class Login extends Model
{
  protected static $tb = [
    'sys_user'    => 'sys_user',
    'sys_role_permission'    => 'sys_role_permission',
    'sys_permission'    => 'sys_permission',
  ];

    /**
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * 小程序
     * */

   /***
     * 后台登陆
     * @param array $data
     * @return bool
     */
    public function checkLogin($data=[]){
        $user = session("user_id");
        if($user){
           return true;
        }else{
          $where = [
            "username"=>$data['username'],
            "password"=>md5($data['password'])
          ];
          $result = Db::name(self::$tb['sys_user'])->where($where)->field("id,name,username,phone,sys_role_id")->find();
          if($result){
            if($data['username'] =='admin'){
              $permission = Db::name(self::$tb['sys_permission'])->field("val")->select(); 
            }else{
            $permission = Db::name(self::$tb['sys_role_permission'])->alias("rp")
            ->join("jun_sys_permission p","p.id=rp.sys_permission_id","left")->where("rp.sys_role_id",$result['sys_role_id'])->field("p.val")->select();    
            } 
            //缓存所有的权限
            if($permission){
              $arr = array_column($permission, 'val'); 
              set_cache("user_".$result['id'],$arr);
              session("user_id",$result);
              return true;
            }
          }else{
            return false;
          }
        }
    }


    /***
     * 小程序登陆获取用户授权信息
     * @param int $web_id
     * @return bool
     */
    public function getUserInfo($data=[]){
  	$res = model("api/Mini")->getUserInfo($data);
  	if(!empty($res['errcode'])){
  		return "-".$res['errcode'];
  	}else{
  		return $res;
  	}
  }



    /**
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * 公众号
     * */

}
